package com.rbac.api.filter;
import com.rbac.api.controller.enums.HintMessageEunm;
import com.rbac.api.controller.response.Response;
import com.rbac.api.exception.*;
import com.rbac.i18n.utils.I18nUtils;
import com.rbac.utils.commUtils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ControllerAdvice(annotations = RestController.class)
public class ControllerAdvice0{
    //日志操作
    private static final LogUtil logger = LogUtil.getLogger(ControllerAdvice0.class);

    //普通异常类
    @ExceptionHandler(AdminAllException.class)
    public ResponseEntity<Object> allException(AdminAllException exception) {
        //响应异常到客户端
        return new ResponseEntity<>(
                new Response(exception.getHintMessageEunm().getId(),
                        StringUtils.trimToEmpty(exception.getHintMessageEunm().getI18nMsg(null))
                ),
                HttpStatus.OK);
    }



    //拦截服务器400错误
    @ExceptionHandler({HttpMessageNotReadableException.class})
    @ResponseBody
    public ResponseEntity<Object> requestNotReadable(HttpMessageNotReadableException ex){
        //LOG
        logger.error("拦截服务器400错误：",ex);
        //响应异常到客户端
        return new ResponseEntity<>(
                new Response(HintMessageEunm.EXCEPTION_SERVICE_FOUR_HUNDRED_ERROR.getId(),
                        StringUtils.trimToEmpty(HintMessageEunm.EXCEPTION_SERVICE_FOUR_HUNDRED_ERROR.getName())
                ),
                HttpStatus.OK);
    }


    /**
     * 服务器异常，包括 500 错误
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handleServiceException(Exception ex) {
        //LOG
        logger.error("httpStatusCode："+HttpStatus.INTERNAL_SERVER_ERROR,ex);
        //
        return new ResponseEntity<>(
                new Response(HintMessageEunm.EXCEPTION_SERVICE_ABNORMAL_ERROR.getId(),
                        StringUtils.trimToEmpty(HintMessageEunm.EXCEPTION_SERVICE_ABNORMAL_ERROR.getName())
                ),
                HttpStatus.OK);
    }





    //拦截校验类的异常错误
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Object validationError(MethodArgumentNotValidException ex) {
        return getLastError(ex.getBindingResult());
    }


    //上面拦截校验类的异常错误类用到的拼接方法
    private ResponseEntity getLastError(BindingResult result) {

        final List<FieldError> errors = result.getFieldErrors();
        StringBuffer errorMsg=new StringBuffer();
        errors.stream().forEach(
                x -> errorMsg.append(I18nUtils.getMsg(x.getDefaultMessage())).append(";")
        );

        return new ResponseEntity<Object>(
                new Response(HintMessageEunm.EXCEPTION_PARAMETER_DEFAULT_ERROR.getId(), errorMsg.toString()),
                HttpStatus.OK);
    }




}